package com.gzk.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gzk.domain.ProductOrder;
import com.gzk.hrm.util.AjaxResult;
import com.gzk.hrm.util.PageList;
import com.gzk.query.ProductOrderQuery;
import com.gzk.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class ProductOrderController {
    @Autowired
    public IProductOrderService productOrderService;

    /**
    * 保存和修改公用的
    * @param productOrder  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody ProductOrder productOrder){
        try {
            if(productOrder.getId()!=null){
                productOrderService.updateById(productOrder);
            }else{
                productOrderService.insert(productOrder);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productOrderService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ProductOrder get(@PathVariable("id")Long id)
    {
        return productOrderService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<ProductOrder> list(){

        return productOrderService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<ProductOrder> json(@RequestBody ProductOrderQuery query)
    {
        Page<ProductOrder> page = new Page<ProductOrder>(query.getPage(),query.getRows());
        page = productOrderService.selectPage(page);
        return new PageList<ProductOrder>(page.getTotal(),page.getRecords());
    }

    /**
     * 下单
     */
    @RequestMapping(value = "/deal",method = RequestMethod.POST)
    public AjaxResult deal(@RequestBody Map<String,Long> map){
        try {
            String orderNo = productOrderService.submitOrder(map);
            return AjaxResult.me().setResultObj(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }

    }
}
